package com.kk.observer.observer;

import java.util.ArrayList;
import java.util.List;

/**
 * @author o_o
 */
public class WeatherData implements Subject {
    /**
     * 1.气温
     */
    private float temperature;
    /**
     * 2.气压
     */
    private float pressure;
    /**
     * 3.湿度
     */
    private float humidity;
    /**
     * 观察者集合
     */
    private List<Observer> observerList;

    public WeatherData() {
        observerList = new ArrayList<>();
    }

    public float getTemperature() {
        return temperature;
    }

    public float getPressure() {
        return pressure;
    }

    public float getHumidity() {
        return humidity;
    }

    public void setWeatherData(float temperature, float pressure, float humidity) {
        this.temperature = temperature;
        this.pressure = pressure;
        this.humidity = humidity;
        notifyObserver();
    }

    @Override
    public void registerObserve(Observer observer) {
        observerList.add(observer);
    }

    @Override
    public void removeObserver(Observer observer) {
        if (observerList.contains(observer)) {
            observerList.remove(observer);
        } else {
            System.out.println(observer + " not in observer list!!!");
        }

    }

    @Override
    public void notifyObserver() {
        for (Observer observer : observerList) {
            observer.update(getTemperature(), getPressure(), getHumidity());
        }
    }
}
